const MS = require('./ms')
const request = require("./request.js")
const {
    isString,
    isUndefined,
    isNumber,
    isBoolean,
} = require('./dataType')

const mediaLibrary = class mediaLibrary {

    constructor() {
    }

    get(id) {
        let params = {
            id: id
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.MEDIA_LIBRARY.GET_MEDIA_LIBRARY.url,
                MS._config.parameters.MEDIA_LIBRARY.GET_MEDIA_LIBRARY.method, params, {}).then(res => {
                if (res.success) {
                    resolve(res.data)
                } else {
                    reject(res.message)
                }
            }).catch(err => {
                reject(err)
            })
        })
    }

    query(options) {
        let params = {
            pageNum: 1,
            pageSize: 10
        }
        if (!isUndefined(options)) {
            if (!isUndefined(options.title) && isString(options.title)) {
                params.title = options.title
            }
            if (!isUndefined(options.status) && isBoolean(options.status)) {
                params.status = options.status
            }
            if (!isUndefined(options.pageNum) && isNumber(options.pageNum)) {
                params.pageNum = options.pageNum
            }
            if (!isUndefined(options.pageSize) && isNumber(options.pageSize)) {
                params.pageSize = options.pageSize
            }
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.MEDIA_LIBRARY.FIND_MEDIA_LIBRARY.url,
                MS._config.parameters.MEDIA_LIBRARY.FIND_MEDIA_LIBRARY.method, params, {}).then(res => {
                if (res.success) {
                    resolve(res.data)
                } else {
                    reject(res.message)
                }
            }).catch(err => {
                reject(err)
            })
        })
    }

    queryPO(options){
        let params = {
            pageNum: 1,
            pageSize: 10
        }
        if (!isUndefined(options)) {
            if (!isUndefined(options.title) && isString(options.title)) {
                params.title = options.title
            }
            if (!isUndefined(options.status) && isBoolean(options.status)) {
                params.status = options.status
            }
            if (!isUndefined(options.pageNum) && isNumber(options.pageNum)) {
                params.pageNum = options.pageNum
            }
            if (!isUndefined(options.pageSize) && isNumber(options.pageSize)) {
                params.pageSize = options.pageSize
            }
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.MEDIA_LIBRARY.FIND_MEDIA_LIBRARY_PO.url,
                MS._config.parameters.MEDIA_LIBRARY.FIND_MEDIA_LIBRARY_PO.method, params, {}).then(res => {
                if (res.success) {
                    resolve(res.data)
                } else {
                    reject(res.message)
                }
            }).catch(err => {
                reject(err)
            })
        })
    }
}

module.exports = mediaLibrary